<template>
  <div class="login">
    <section class="form_container">
      <div class="manage_tip">
        <span class="title">公租房管理系统</span>
        <el-form :model="loginUser" :rules="rules" ref="loginForm" label-width="60px" class="loginForm">
          <el-form-item label="账号" prop="account">
            <el-input v-model="loginUser.account" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <!--绑定回车事件-->
            <el-input v-model="loginUser.password" placeholder="请输入密码" type="password"
                      @keyup.enter.native="doLogin('loginForm')"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="submit_btn" @click="doLogin('loginForm')">登 录</el-button>
            <el-button type="success" class="submit_btn" @click="doRegister()">注 册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </section>

    <LoginRegisterDialog :dialog0="dialog0"></LoginRegisterDialog>
  </div>
</template>

<script>
import jwt_decode from 'jwt-decode'
import userApi from '../api/user'
import {setUser} from '../utils/auth'
import paidApi from '../api/paid.js'
import scheduleApi from '../api/schedule.js'
import LoginRegisterDialog from "../components/LoginRegisterDialog";

export default {
  name: 'login',
  components: {
    LoginRegisterDialog
  },
  data() {
    return {
      notifyPromise: Promise.resolve(),
      loginUser: {
        account: '',
        password: ''
      },
      rules: {
        account: [
          {
            required: true,
            message: '账号不能为空',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur"
          }
        ]
      },
      dialog0: {
        show: false,
        title: ""
      }
    }
  },
  methods: {
    //通知，解决element-ui，同时调用notify时，通知重叠的问题
    scheduleNotify(msg) {
      this.notifyPromise = this.notifyPromise.then(this.$nextTick).then(() => {
        this.$notify.info({
          title: "公告",
          message: msg,
          offset: 80
        });
      })
    },
    paidNotify(msg) {
      this.notifyPromise = this.notifyPromise.then(this.$nextTick).then(() => {
        this.$notify.info({
          title: "缴费通知",
          message: msg,
          offset: 80,
          type: 'warning',
          duration: 0
        });
      })
    },
    isEmpty(value) {
      return (
          value === undefined ||
          value === null ||
          (typeof value === "object" && Object.keys(value).length === 0) ||
          (typeof value === "string" && value.trim().length === 0)
      );
    },
    doLogin(formName) {
      let pojo = {
        account: this.loginUser.account,
        password: this.loginUser.password
      }
      userApi.login(pojo).then(res => {
        //登录成功之后的处理
        if (res.data.flag) {

          let systemRole = res.data.systemRole
          let id = res.data.userInfo.id
          let name = res.data.userInfo.name
          let token = res.data.token

          //将用户信息存入cookie中
          setUser(systemRole, name, id, token)

          const decode = jwt_decode(token);

          // 存储数据
          this.$store.dispatch("setIsAutnenticated", !this.isEmpty(decode));
          this.$store.dispatch("setUser", decode);

          this.$message({
            message: "登录成功",
            type: "success"
          });
          this.$router.push("/index")
          scheduleApi.getSchedule().then(res => {
            if (res.data.flag) {
              if (res.data.data.length != 0) {
                for (var i = 0; i < res.data.data.length; i++) {
                  this.scheduleNotify(res.data.data[i].content)
                }
              }
            } else {
              this.$message({
                message: '获取公告失败',
                type: 'warning'
              });
            }
          })
          let pojo = {
            userlist_id: this.$store.getters.user.jti
          }
          paidApi.getPaidListByCondition(pojo).then(res => {
            if (res.data.flag == true) {
              if (res.data.data.length != 0) {
                for (var i = 0; i < res.data.data.length; i++) {
                  if (res.data.data[i].price != 0) {
                    this.paidNotify("您在" + res.data.data[i].address + "还有 " + res.data.data[i].price + " 元未缴，请及时缴清。");
                  }
                }
              }
            } else {
              this.$message({
                message: '获取租金信息列表失败',
                type: 'warning'
              });
            }
          })

        } else {
          this.$message({
            message: "账号或密码错误",
            type: 'error'
          })
          this.loginUser.account = ''
          this.loginUser.password = ''
        }
      })
    },
    doRegister() {
      this.dialog0 = {
        show: true,
        title: "添加用户信息"
      };
    }
  }
}
</script>

<style scoped>
.login {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(../assets/bg.jpg) no-repeat center center;
  background-size: 100% 100%;
}

.form_container {
  width: 370px;
  height: 210px;
  position: absolute;
  left: calc(50% - 225px);
  top: calc(50% - 190px);
  padding: 25px;
  /*border-radius: 5px;*/
  text-align: center;
}

.form_container .manage_tip .title {
  font-family: "Microsoft YaHei";
  font-weight: bold;
  font-size: 26px;
  color: #fff;
}

.loginForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 40px 40px 10px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}

.submit_btn {
  width: 48%;
}
</style>

